import {action, makeAutoObservable, observable} from "mobx";
import {AxioInstance} from "../api/AxiosHelper";
import {ADD_COURSE_COMMENT, GET_COURSE_COMMENT} from "../api/APIURLDefine";

/*评论*/
export default class CommentStore {
    constructor() {
        makeAutoObservable(this)
    }

    @observable comments = null;

    @action SetComments(value: any) {
        this.comments = value;
    }

    /*当前课程的id*/
    @observable courseId = 0;

    @action SetCourseId(value: number) {
        this.courseId = value;
        this.getComments();
    }

    /*当前回复的用户ID*/
    @observable replyId = 0;

    @action SetReplyId(value: number) {
        this.replyId = value;
    }

    /*当前评论的内容*/
    @observable comment = "";

    @action SetComment(value: string) {
        this.comment = value;
    }

    addComment(content: string, reply: number, userId: number) {
        let comment = {courseId: this.courseId, content: content, replyTo: reply, status: 1, userId: userId}
        console.log(comment)
        let msg = JSON.stringify(comment);
        AxioInstance.post(ADD_COURSE_COMMENT, msg).then((resp) => {
            //console.log(resp);
            this.getComments();
            this.SetReply("");
            this.SetReplyId(0);
            this.SetComment("");
        }).catch((e) => console.error(e));
    }

    getComments = () => {
        AxioInstance.get(`${GET_COURSE_COMMENT}/1/99?id=${this.courseId}`).then((resp: any) => {
            //console.log(resp);
            this.SetComments(resp.records);
            // this.setState({comments: resp.records});
        }).catch((e) => console.error(e));
    }
    /*当前回复的内容*/
    @observable reply = "";

    @action SetReply(value: string) {
        this.reply = value;
    }
}